<?php
class Upload{
	/**
     * 默认上传配置
     * @var array
     */
    private $config = array(
        'mimes'         =>  array(), //允许上传的文件MiMe类型
        'maxSize'       =>  0, //上传的文件大小限制 (0-不做限制)
        'exts'          =>  array(), //允许上传的文件后缀
        'rootPath'      =>  './uploads/', //保存根路径
        'savePath'		=>'',//保存路径
        'pathFormat'	=>1,//1:默认格式2:Y/m/d格式3:Y_m_d
        'maxwidth'		=>'',//最大宽带
        'maxheight'		=>'',//最大高度
        'isdel'			=>1,//是否删除同名文件
        'uppath'		=>'',//
    );
    /*public function setconfig($name,$value){
		$this->config[$name]=$value;
	}*/
	public function setconfig($config){
		foreach($config as $k=>$v){
			$this->config[$v[0]]=$v[1];
		}
	}

	/**
     * 获取最后一次上传错误信息
     * @return string 错误信息
     */
    public function getError(){
        return $this->upfileerror;
    }
    //单张上传
	public function upload_one($file){
		$files[]=$file;
		return $this->uploads($files);
	}
	//上传目录路径
	private function dir_pathFormat(){
		$uppath='';
		if(!empty($this->config['uppath'])){//固定到上传路径
			$uppath=$this->config['uppath'];
		}else{// 默认设置 和 生成
			 /* 检测上传根目录 */
		    $rootpath=$this->config['rootPath'];
		    if(!is_dir($rootpath)){
	          	if(!mkdir($rootpath,0777,true)){
	    			$this->upfileerror="上传文件目录".$rootpath."创建失败";
	    			return false;
	    		}
			}
			//生成目录结构
	    	if($this->config['pathFormat']==1){
				$savePath=$this->config['savePath'].'/';
			}else if($this->config['pathFormat']==2){
				 $savePath=$this->config['savePath'].'/'.date('Y').'/'.date('m')."/".date('d')."/";
			}else if($this->config['pathFormat']==3){
				$savePath=$this->config['savePath'].'/'.date('Y_m_d').'/';
			}
			$uppath=$rootpath.$savePath;
		}
		if(!is_dir($uppath)){
			if(!mkdir($uppath,0777,true)){
				$this->upfileerror="上传文件目录".$uppath."创建失败";
				return false;
			}
		}
		return $uppath;
	}
	/**
     * 上传文件
     * @param 文件信息数组 $files ，通常是 $_FILES数组
     */
    public function uploads($files=''){
        if('' === $files){
            $files  =   $_FILES;
        }
        if(empty($files)){
            $this->upfileerror = '没有上传的文件！';
            return false;
        }

		 /* 逐个检测并上传文件 */
        /*$info    =  array();
        if(function_exists('finfo_open')){
            $finfo   =  finfo_open ( FILEINFO_MIME_TYPE );
        }*/
		$files=$this->filesarry($files);
		foreach($files as $key=>$file){
			/*if(isset($finfo)){
                $file['type']=finfo_file($finfo,$file['tmp_name']);
            }*/
			if(empty($file['name'])){
				$this->upfileerror='未知错误';
				return false;
			}
			if($file['error']!=0){
				$this->upfileerror ="上传文件失败,error错误值为".$file['error'];
				return false;
			}
			if (!is_uploaded_file($file['tmp_name'])) {
            	$this->upfileerror ="上传文件错误error值为".$file['error'];
            	return false;
        	}
        	//文件大小限定
        	if($this->config['maxSize']!=0){
				if($file['size']>$this->config['maxSize']*1024*1024){
					$this->upfileerror="上传文件不超过".$this->config['maxSize']."MB";
					return false;
				}
			}
			/* 检查文件Mime类型 */
        	//FLASH上传的文件获取到的mime类型都为application/octet-stream
	        if (!empty($this->config['mimes'])){
	        	if(!in_array($file['type'],$this->config['mimes'])){
					$this->upfileerror = '上传文件MIME类型不允许！';
	            	return false;
				}
	        }
	         $file['exts']    =   pathinfo($file['name'], PATHINFO_EXTENSION);
	        //文件后缀
	        if (!empty($this->config['exts'])){
	        	if(!in_array($file['exts'],$this->config['exts'])){
	        		$exts=implode(',',$this->config['exts']);
					       $this->upfileerror = '只允许上传'.$exts."的文件";
	            	return false;
				}
	        }
	        //获取上传目录
	        $path=$this->dir_pathFormat();
            if($path===false){return false;}
			$filename=$path.$file['name'];
	       /* 不覆盖同名文件 */
        	if (is_file($filename) && $this->config['isdel']==false) {
           		$this->upfileerror = '存在同名文件' . $file['name'];
            	return false;
        	}
	        /* 移动文件 */
        	if (!move_uploaded_file($file['tmp_name'],mb_convert_encoding($filename,'gbk','utf-8'))){
            	$this->upfileerror = '文件上传保存错误！';
           	 	return false;
        	}
        	if($this->config['maxwidth']!=''){
				$filename_arr = getimagesize($filename);
				$filename_w= $filename_arr[0];
				if($filename_w>$this->config['maxwidth']){
					unlink($filename);
					$this->upfileerror='上传图片最大宽带不能大于'.$this->config['maxwidth'];
					return false;
				}
			}
			if($this->config['maxheight']!=''){
				$filename_arr = getimagesize($filename);
				$filename_w= $filename_arr[1];
				if($filename_w>$this->config['maxheight']){
					unlink($filename);
					$this->upfileerror='上传图片最大高度不能大于'.$this->config['maxheight'];
					return false;
				}
			}
        	$info[$key]=$filename;
		}
        return $info;
    }
    //数据化处理
    private function filesarry($files){
    	$arr=array();
    	$count=count($files);
		for($i=0;$i<$count;$i++){
			$arr[$i]['name']=$files[$i]['name'];
			$arr[$i]['type']=$files[$i]['type'];
			$arr[$i]['tmp_name']=$files[$i]['tmp_name'];
			$arr[$i]['error']=$files[$i]['error'];
			$arr[$i]['size']=$files[$i]['size'];
		}
		return $arr;
	}
}
/*$upload=new Upload();
		$upload->savePath('test');
		$file=$_FILES['spic'];

		if(!$res=$upload->uploads($file)){
			echo $upload->getError();
		};
		print_r($res);*/
?>
